package org.hnxxxy.ebms.controller;


import com.baomidou.mybatisplus.core.metadata.IPage;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.hnxxxy.ebms.common.Result;
import org.hnxxxy.ebms.pojo.Permission;
import org.hnxxxy.ebms.pojo.RolePermission;
import org.hnxxxy.ebms.service.PermissionService;
import org.hnxxxy.ebms.service.RolePermissionService;
import org.hnxxxy.ebms.vo.StuRolePermissionVo;
import org.hnxxxy.ebms.vo.TeaRolePermissionVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author WANGWEI
 * @since 2021-06-10
 */
@RestController
@RequestMapping("//permission")
public class PermissionController {

    @Autowired
    private RolePermissionService rolePermissionService;

    @Autowired
    private PermissionService permissionService;

    @PostMapping("/add")
    @ApiOperation("/添加角色权限")
    public Result<String> add(@RequestBody RolePermission rolePermission){
        if (rolePermissionService.save(rolePermission)) {
            return Result.success(200,"添加成功");
        }
        return Result.fail("添加失败");
    }

    @DeleteMapping("/del")
    @ApiOperation(value = "删除角色权限")
    public Result<String> del(Integer roleId,Integer permissionId){
        return rolePermissionService.del(roleId,permissionId);
    }

    @GetMapping("/get/stu/{page}")
    @ApiOperation(value = "获取学生角色权限")
    public Result<IPage<StuRolePermissionVo>> get(@PathVariable("page") int page){
        return rolePermissionService.getStuPermission(page);
    }

    @GetMapping("/get/tea/{page}")
    @ApiOperation(value = "获取老师角色权限")
    public Result<IPage<TeaRolePermissionVo>> getTeaPer(@PathVariable("page") int page){
        return rolePermissionService.getTeaPermission(page);
    }

    @PostMapping("/edit/stu")
    @ApiOperation(value = "修改学生权限")
    public Result<String> editRecPer(@RequestBody StuRolePermissionVo stuRolePermissionVo){
        return rolePermissionService.editRecPer(stuRolePermissionVo);
    }

    @PostMapping("/edit/tea")
    @ApiOperation(value = "修改老师权限")
    public Result<String> editRecPer(@RequestBody TeaRolePermissionVo teaRolePermissionVo){
        return rolePermissionService.editTeaPer(teaRolePermissionVo);
    }

    @GetMapping("/getName/stu/{page}/{name}")
    @ApiOperation(value = "根据姓名查询学生权限")
    public Result<IPage<StuRolePermissionVo>> getStuPerName(@PathVariable("name") String name,@PathVariable("page") int page){
        return rolePermissionService.getStuPerName(name,page);
    }

    @GetMapping("/getName/tea/{page}/{name}")
    @ApiOperation(value = "根据姓名查询学生权限")
    public Result<IPage<TeaRolePermissionVo>> getTeaPerName(@PathVariable("name") String name,@PathVariable("page") int page){
        return rolePermissionService.getTeaPerName(name,page);
    }

    @GetMapping("/get/per")
    @ApiOperation(value = "获取权限列表")
    public Result<List<Permission>> getPer(){
        List<Permission> list = permissionService.list();
        return Result.success(list);
    }

}
